package com.BigDate.server.service.impl;

import com.BigDate.server.mapper.ProjectCommentMapper;
import com.BigDate.server.pojo.Comment;
import com.BigDate.server.pojo.CommentNodeTreeVo;
import com.BigDate.server.service.ProjectCommentService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProjectCommentServiceImpl extends ServiceImpl<ProjectCommentMapper, CommentNodeTreeVo> implements ProjectCommentService {

    @Autowired
    private ProjectCommentMapper commmentMapper;

    /**
     * 递归进行求树形节点
     *
     * @param commentNodeTreeVo 评论树
     */
    private void deepSearch(CommentNodeTreeVo commentNodeTreeVo) {
        if (commentNodeTreeVo != null) {
            // 查询子评论集合
            List<CommentNodeTreeVo> commentNodeTreeVos = commmentMapper.findChildComment(commentNodeTreeVo.getComment_id());
            if (!CollectionUtils.isEmpty(commentNodeTreeVos)) {
                for (CommentNodeTreeVo vo : commentNodeTreeVos) {
                    commentNodeTreeVo.setCommentNodeTreeVos(commentNodeTreeVos);
                    deepSearch(vo);
                }
            }
        }
    }

    @Override
    public List<CommentNodeTreeVo> findComment(Integer projectId) {
        return commmentMapper.findComment(projectId);
    }

    @Override
    public List<CommentNodeTreeVo> findChildComment(Integer commentId) {
        return commmentMapper.findChildComment(commentId);
    }

    @Override
    public int add(Comment comment) {
        return commmentMapper.add(comment);
    }

    @Override
    public Page<CommentNodeTreeVo> findAll(Integer current,Integer size)
    {
        Page<CommentNodeTreeVo> page = new Page<>(current, size);
        page.setRecords(commmentMapper.findAll(page));
        return page;
    }

    @Override
    public int deleteById(Integer id) {
        return commmentMapper.deleteById(id);
    }


}
